package com.study.it.xw.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.it.xw.dao.CommentDao;
import com.study.it.xw.dao.UserDao;
import com.study.it.xw.dao.WeiBoDao;
import com.study.it.xw.entity.Comment;
import com.study.it.xw.entity.MyWebConfig;
import com.study.it.xw.entity.User;
import com.study.it.xw.entity.WeiBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class WeiBoController {

    @Autowired
    private WeiBoDao weiBoDao;
    @Autowired
    private CommentDao cmDao;
    @Autowired
    private UserDao userDao;



    @GetMapping("/detail")
    public String searchDetail(int wbid,Model m){
        WeiBo wb=weiBoDao.selectById(wbid);
        User author=userDao.selectById(wb.getUserId());
        int wbCount=weiBoDao.selectWbCount(wb.getUserId());
        List<Comment> commentList=cmDao.selectByWbId(wbid);

        m.addAttribute("wb",wb);
        m.addAttribute("author",author);
        m.addAttribute("wbCount",wbCount);
        m.addAttribute("comments",commentList);
        return "detail";
    }

    @GetMapping("/he")
    public String searchHe(int uid,Model m){
        List<WeiBo> list=weiBoDao.selectByUser(uid);
        m.addAttribute("weibos",list);
        return "list";
    }






    //    //获取主页
    @GetMapping("/index")
//    页面传入想看第几页，如果没有传，默认是第一页
    public String searchAll(@RequestParam(value = "page",defaultValue = "1") int page
                                        ,Model m){
        PageHelper.startPage(page,8).setOrderBy("wb_createTime desc");

        List<WeiBo> list=weiBoDao.selectAll();
        //把查询结果放进PageInfo对其进行分页
        PageInfo<WeiBo> pageInfo=new PageInfo<WeiBo>(list);
        m.addAttribute("weibos",pageInfo);
        return "list";
    }

    @GetMapping("/add")
    public String add(){
        return "add";
    }


    @Value("${my.upload}")
    private String uploadDir;

    @PostMapping("/add")
    public String add(WeiBo wb, HttpSession session, MultipartFile imgfile)throws Exception{
        User u=(User)session.getAttribute("user");
        wb.setUserId(u.getId());
        wb.setCreatetime(new Date());
        wb.setReadcount(0);

        //处理图片
        if(!imgfile.isEmpty()){
            String oldname=imgfile.getOriginalFilename();
            String ext=oldname.substring(oldname.lastIndexOf("."));
            String filename= UUID.randomUUID().toString()+ext;
            imgfile.transferTo(new File(this.uploadDir+"/"+filename));
            wb.setImg(filename);
        }

        weiBoDao.insertWeiBo(wb);
        return "redirect:index";
    }

//    @GetMapping("/list")
//    public String list(){
//        weiBoDao.selectAll();
//        return "list";
//    }

}
